#!/usr/bin/env python
# dsx is Mandrakesoft's Xtart modified to work with a Gentoo Linux system.
# Gentoo doesn't use a wmsession.d directory, so we're going to accept the
# /etc/X11/Sessions executables as our menu and our command parameters.
# I also tried to increase the robustness of the entry checking and make the
# error messages less condescending.
# The 0 option for "no wm" was removed.  I altered it to run option 1 instead.

### Original copyright / license message from Xtart... ###
#  This is an official linux-mandrake extension of the X windowing system for
#  the benefit of Declan Moriarty  Copyright 2001 by civileme@mandrakesoft all
#  rights reserved.  Use is hereby granted under the GNU General Publoic
#  License version 2 or any future version of said license at the user's option
#
#  No Warranty of course.
#  This program is designed for those who want to work in concole and   
#  occasionally boot into window managers or who want to avoid theme
#  persistence from one WM to another.
################
import sys
import os
import popen2
import string
import math
def complain():
    print 'X initiation aborted on invalid selection.'
    sys.exit(0)
    return
print
print 'Welcome to the Desktop Selector for X'
print
fspc=os.path.expanduser('~')+'/'
io=popen2.Popen3('ls /etc/X11/Sessions',1)
L=io.fromchild.readlines()
Sessions={}
Sessionkeys=[]
primekey=0
for k in L:
    primekey+=1
    name=k[:-1]
    command='/etc/X11/Sessions/'+k[:-1]
    Sessions[primekey]=(name, command)
Sessionkeys=Sessions.keys()
Sessionkeys.sort()
for j in Sessionkeys:
    if os.system('[ -x '+Sessions[j][1]+' ]') == 0:
        print j, Sessions[j][0]
    pass
print
sys.stdin.flush()
s=raw_input('Enter the number of your desired desktop environment [1]: ')
if len(s)==0:
    s='1'
if s=='0':
    s='1'
for j in range(len(s)):
    if string.find(string.digits,s[j]) == -1:
        print "'" + s[j] + "' isn't a digit."
	complain()
if len(s)>int(math.log10(primekey))+1:
    print "That number is too big."
    complain()
well=int(s)
wellformed=Sessions.has_key(well)
if wellformed:
    os.execvp('xinit',Sessions[well])
else:
    print
    print 'Your selection was invalid.'
    print
    complain()

